#include "arch_os.h"
#define ARCH_OS_MS2TICK(ms)      ((ms)/portTICK_PERIOD_MS)
#define ARCH_OS_TICK2MS(tick)    ((tick)*portTICK_PERIOD_MS)

static uint32_t tick_high = 0;			//tick高位 添加高位用于扩展计数范围
static bool tick_half_flag = false;		//tick值过半标记 超过0x7FFFFFFF 便设置为1


uint32_t arch_os_tick_now( void )
{
	uint32_t tick = xTaskGetTickCount();

	if(tick > 0x7FFFFFFF)
		tick_half_flag = true;	//过半标记

	else if(true == tick_half_flag){	//若溢出 还没有标记
		tick_half_flag = false;
		tick_high++;
	}

	return tick;
}


uint32_t arch_os_tick_elapsed(uint32_t last_tick)
{
	uint32_t now = arch_os_tick_now();
	
	if(last_tick  < now)
		return now - last_tick;
	else//可能溢出
		return 0xFFFFFFFF - last_tick + now + 1;
}



uint32_t arch_os_ms_now( void )
{
    return (ARCH_OS_TICK2MS(arch_os_tick_now()));
}


uint32_t arch_os_ms_elapsed(uint32_t last_ms)
{
    return (ARCH_OS_TICK2MS(arch_os_tick_elapsed(ARCH_OS_MS2TICK(last_ms))));
}